﻿using System;
using System.Text;
using SecretLabs.NETMF.Hardware;
using Netduino.Sensor.Unit;

namespace Netduino.Sensor
{
    /// <summary>
    /// A sensor for measuring acceleration on one axis, using the ADXL 335 Breakout board from Sparkfun.
    /// </summary>
    class Adxl335GSensorAxis : ISensor
    {

        /// <summary>
        /// The unit to use
        /// </summary>
        private SpecifiedUnit _unit;

        /// <summary>
        /// Creates a new instance, using the specified pin as the specified axis.
        /// </summary>
        /// <param name="pin"></param>
        /// <param name="axisCaption"></param>
        public Adxl335GSensorAxis(Microsoft.SPOT.Hardware.Cpu.Pin pin, String axisCaption)
        {
            Input = new AnalogInput(pin);
            AxisCaption = axisCaption;

            _unit = new SpecifiedUnit(
                    "Earth acceleration (" + AxisCaption + ")",
                    "g (" + AxisCaption + ")"
                    );
        }

        /// <summary>
        /// Takes a new sample.
        /// </summary>
        /// <returns></returns>
        public ISample Take()
        {
            return new Sample(Input.Read(), _unit);
        }

        /// <summary>
        /// Return the caption for the g sensor axis.
        /// </summary>
        public string Caption
        {
            get { return _unit.Name; }
        }

        /// <summary>
        /// The caption of the axis that the observed pin represents.
        /// </summary>
        private string AxisCaption { get; set; }

        /// <summary>
        /// The input to use for measuring the acceleration
        /// </summary>
        private AnalogInput Input { get; set; }
    }
}
